﻿using ElectronicShoppingMall.Service.Data;
using ElectronicShoppingMall.Service.Models.Enterprise;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElectronicShoppingMall.Service.Services.Enterprise
{
   public  class MenuService : IDisposable
    {
        private ElectronicShoppingMallContext data;
        private List<MenuModel> menus = new List<MenuModel>()
            {
                new MenuModel { MenuId = 1, MenuName = "订单中心" ,ParentId = 0,PermissionCode ="OrderCenter",Route = "0"},
                new MenuModel { MenuId = 2, MenuName = "商品中心" ,ParentId = 0,PermissionCode ="CommodityCenter",Route = "0"},
                new MenuModel { MenuId = 3, MenuName = "库存中心" ,ParentId = 0,PermissionCode ="InventoryCenter",Route = "0"},
                new MenuModel { MenuId = 4, MenuName = "广告中心" ,ParentId = 0,PermissionCode ="AdvertisingCenter",Route = "0"},
                new MenuModel { MenuId =5, MenuName = "角色中心" ,ParentId = 0,PermissionCode ="RoleCenter",Route = "0"},
                new MenuModel { MenuId =6, MenuName = "退货处理" ,ParentId = 1,PermissionCode ="OrderCenter/ReturnGoodsHandling",Route = "ReturnGoodsHandling"},
                new MenuModel { MenuId =7, MenuName = "退款处理" ,ParentId = 1,PermissionCode ="OrderCenter/RefundProcessing",Route = "RefundProcessing"},
                new MenuModel { MenuId = 8, MenuName = "商品发货" ,ParentId = 1,PermissionCode ="OrderCenter/DeliverGoods",Route = "DeliverGoods"},
                new MenuModel { MenuId = 9, MenuName = "商品上下架" ,ParentId = 2,PermissionCode ="CommodityCenter/CommodityUpperAndLowerFrame",Route = "CommodityUpperAndLowerFrame"},
                new MenuModel { MenuId = 10, MenuName = "商品信息修改" ,ParentId = 2,PermissionCode ="CommodityCenter/ModificationOfCommodityInformation",Route = "ModificationOfCommodityInformation"},
                new MenuModel { MenuId = 11,MenuName = "新货入库" ,ParentId = 3,PermissionCode ="InventoryCenter/InNewStocks",Route = "InNewStocks"},
                new MenuModel { MenuId = 12, MenuName = "退货入库" ,ParentId = 3,PermissionCode ="InventoryCenter/ReturnWarehousing",Route = "ReturnWarehousing"},
                new MenuModel { MenuId = 13, MenuName = "确认发货" ,ParentId = 3,PermissionCode ="InventoryCenter/ConfirmationOfDelivery",Route = "ConfirmationOfDelivery"},
                new MenuModel { MenuId = 14, MenuName = "角色管理" ,ParentId = 5,PermissionCode ="RoleCenter/RoleManagement",Route = "RoleManagement"},
                new MenuModel { MenuId = 15, MenuName = "用户管理" ,ParentId = 5,PermissionCode ="RoleCenter/UserManagement",Route = "UserManagement"},
                new MenuModel { MenuId = 16, MenuName = "新品推荐管理" ,ParentId = 4,PermissionCode ="AdvertisingCenter/NewProductRecommendationManagement",Route = "NewProductRecommendationManagement"},
                  new MenuModel { MenuId = 17, MenuName = "操作日志查看" ,ParentId = 5,PermissionCode ="RoleCenter/OperationLogView",Route = "OperationLogView"}
            };
        public MenuService()
        {
            data = new ElectronicShoppingMallContext();
        }

        public void Dispose()
        {
            data.Dispose();
        }

        public List<Permission> GetMenuPermission(int EmployeeId)
        {
            Employee currentEmployee = data.Employees.SingleOrDefault(u => u.EmployeeId== EmployeeId);
            List<Permission> userPermissions = new List<Permission>();
            List<Role> currentUserRoles = currentEmployee.Roles.ToList();
            List<Permission> currentUserRolesPermission = new List<Permission>();
            foreach (var role in currentUserRoles)
            {
                currentUserRolesPermission.AddRange(role.Permissions);
            }
            userPermissions.AddRange(currentEmployee.Permissions.Union(currentUserRolesPermission));
            return userPermissions;
        }

        public List<MenuModel> GetMenuModels(int EmployeeId)
        {
            List<Permission> permission = GetMenuPermission(EmployeeId);
            List<MenuModel> userMenu = new List<MenuModel>();
            for (int i = 0; i < permission.Count; i++)
            {
                for (int j = 0; j < menus.Count; j++)
                {
                    if (permission[i].Code.StartsWith(menus[j].PermissionCode))
                    {
                        if (!userMenu.Contains(menus[j]))
                        {
                            userMenu.Add(menus[j]);
                        }
                    }
                }
            }
            return userMenu;
        }

        public MenuModel CreateMenuModel(int EmployeeId)
        {
            List<MenuModel> menus = GetMenuModels(EmployeeId);
            MenuModel model = new MenuModel();
            //找到根目录   parentId为0的为根目录
            var roots = menus.Where(m => m.ParentId == 0);
            var rootArrays = roots.ToArray();
            for (int i = 0; i < rootArrays.Length; i++)
            {
                Create(rootArrays[i], menus);
            }
            model.MenuId = 0;
            model.MenuName = "男人帮电商后台管理";
            model.Childrens = roots.ToList();
            return model;
        }
        public void Create(MenuModel menuRoot, List<MenuModel> menus)
        {
            menuRoot.Childrens = new List<MenuModel>();
            if (menuRoot != null)
            {
                //找到它的所有子菜单
                IEnumerable<MenuModel> chidrensmenus = menus.Where(m => m.ParentId == menuRoot.MenuId);
                foreach (var item in chidrensmenus)
                {
                    menuRoot.Childrens.Add(item);
                    //添加它的子菜单
                    AddChildNode(item, menus);
                }
            }
        }
        public void AddChildNode(MenuModel menu, List<MenuModel> menus)
        {
            menu.Childrens = new List<MenuModel>();
            List<MenuModel> viewMenus = new List<MenuModel>();
            IEnumerable<MenuModel> childrensMenus = menus.Where(m => m.ParentId == menu.MenuId);
            foreach (var item in childrensMenus)
            {
                menu.Childrens.Add(item);
                AddChildNode(item, menus);
            }
        }
    }
}
